﻿using System;
using System.Collections.Generic;
using System.Security;
using System.Security.Cryptography;
using System.Text;

namespace HelloWorld.Labs.Lab1
{
    class LoginModule
    {
        private const string ENTER_LOGIN_MESSAGE_RUS    = "Введите логин:";
        private const string ENTER_PASSWORD_MESSAGE_RUS = "Введите пароль:";
        private const string LOGIN_DOESNT_EXIST         = "Указанный логин не существует";
        private const string PASSWORD_DOESNT_EXIST      = "Указан неверный пароль";
       
        private static String getPassword()
        {
            StringBuilder pwd = new StringBuilder();
            while (true)
            {
                ConsoleKeyInfo i = Console.ReadKey(true);
                if (i.Key == ConsoleKey.Enter)
                {
                    break;
                }
                else if (i.Key == ConsoleKey.Backspace)
                {
                    if (pwd.Length > 0)
                    {
                        pwd.Remove(pwd.Length - 1,1);
                        Console.Write("\b \b");
                    }
                }
                else
                {
                    pwd.Append(i.KeyChar);
                    Console.Write("*");
                }
            }
            Console.WriteLine();
            return pwd.ToString();
        }

        private static void ShowError(String message)
        {
            ConsoleColor cachedColor = Console.ForegroundColor;
            Console.ForegroundColor  = ConsoleColor.Red;
            Console.WriteLine(message);
            Console.ForegroundColor = cachedColor;
        }

        public static bool IdentifyUser()
        {
            for (;;)
            {
                Console.Write(ENTER_LOGIN_MESSAGE_RUS);
                String login    = Console.ReadLine().ToLower();
                Console.Write(ENTER_PASSWORD_MESSAGE_RUS);
                String password = getPassword().ToLower();

                byte[] encodedPassword = new UTF8Encoding().GetBytes(password);
                byte[] hash            = ((HashAlgorithm)CryptoConfig.CreateFromName("MD5")).ComputeHash(encodedPassword);
                string hashEncoded     = BitConverter.ToString(hash).Replace("-", string.Empty).ToLower();

                    switch (login)
                    {
                        case "user":
                            {
                                if (hashEncoded == "6ad14ba9986e3615423dfca256d04e3f")
                                {
                                    return true;
                                }
                                ShowError(LOGIN_DOESNT_EXIST);
                                break;
                            }
                        case "admin":
                            {
                                if (hashEncoded == "0192023a7bbd73250516f069df18b500")
                                {
                                    return true;
                                }
                                ShowError(LOGIN_DOESNT_EXIST);
                                break;
                            }
                        case "guest":
                            {
                                if (hashEncoded == "fcf41657f02f88137a1bcf068a32c0a3")
                                {
                                    return true;
                                }
                                ShowError(LOGIN_DOESNT_EXIST);
                                break;
                            }
                        default:
                            {
                                ShowError(LOGIN_DOESNT_EXIST);
                                break;
                            }
                    }
                    ShowError("Повторите попытку.");
                    Console.WriteLine("=======================================================");
            }
        }
    }
}
